<template>
  <Dialog title="审核" v-model="dialogVisible">
    <el-form ref="formRef" :model="formData" :rules="formRules" label-width="120px" v-loading="formLoading">
      <el-form-item label="商家状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio v-for="dict in getIntDictOptions(DICT_TYPE.MERCHANT_STATUS)" :key="dict.value" :label="dict.value">
            {{ dict.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="商家认证" prop="isAuth">
        <el-radio-group v-model="formData.isAuth">
          <el-radio v-for="dict in getIntDictOptions(DICT_TYPE.MERCHANT_CERTIFICATION)" :key="dict.value"
            :label="dict.value"> {{ dict.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="认证时间" prop="authTime">
        <el-date-picker v-model="formData.authTime" type="date" value-format="x" placeholder="选择认证时间" />
      </el-form-item>
      <el-form-item label="有效时间" prop="overTime">
        <el-date-picker v-model="formData.overTime" type="date" value-format="x" placeholder="选择有效时间" />
      </el-form-item>

    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { BusinessUserApi, BusinessUserVO } from '@/api/wine/businessuser'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

/** 会员等级管理 表单 */
defineOptions({ name: 'audit' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  isAuth: undefined,
  authTime: undefined,
  overTime: undefined,
  status: undefined,
})
const formRules = reactive({
  isAuth: [{ required: true, message: '商家认证不能为空', trigger: 'change' }],
  //authTime: [{ required: true, message: '认证时间不能为空', trigger: 'change' }],
  // overTime: [{ required: true, message: '有效时间不能为空', trigger: 'change' }],
  status: [{ required: true, message: '商家状态不能为空', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (row) => {
  dialogVisible.value = true
  resetForm()
  formData.value = row
}

defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as BusinessUserVO
    await BusinessUserApi.audit(data)
    message.success(t('common.createSuccess'))
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    isAuth: undefined,
    authTime: undefined,
    overTime: undefined,
    status: undefined,
  }
  formRef.value?.resetFields()
}
</script>
